import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.Utils import ExamDriver


#测试考试登录界面
class ExamLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = ExamDriver.driver
        self.url = "https://gms.test.tgtrain.cn/"
        self.driver.get(self.url)

    #登录失败的测试用例
    def LoginFailTest(self):
        #显示等待
        login_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "#app > div > div.right > div.main > div:nth-child(3) > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input"))
        )
        self.driver.find_element(By.CSS_SELECTOR,"#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys("12")
        self.driver.find_element(By.CSS_SELECTOR,"#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys("12345678")
        self.driver.find_element(By.CSS_SELECTOR,"#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()

        username_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(username_element.text)
        assert username_element.text == "账号至少3个字符"

        # 密码过短
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys(
            "123")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys(
            "1234567")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()
        time.sleep(1)
        password_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(password_element.text)
        assert password_element.text == "密码不能少于8位！"

        #账号和密码长度都异常
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys(
            "12")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys(
            "1234567")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()
        time.sleep(1)
        username_password_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(username_password_element.text)
        assert username_password_element.text == "账号至少3个字符"

        #账号密码为空的情况
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()

        username_password_empty = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(username_password_empty.text)
        assert username_password_empty.text == "请输入账号！"

        #账号不为空、密码为空
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys(
            "12")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()

        password_empty = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(password_empty.text)
        assert password_empty.text == "请输入密码"

        #账号为空、密码不为空
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys(
            "1234567")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()

        username_empty = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        )
        print(username_empty.text)
        assert username_empty.text == "请输入账号！"

        # #账号密码错误（账号：”123“，密码：”12345678“）
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()
        #
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys(
        #     "123")
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys(
        #     "12345678")
        #
        # time.sleep(3)
        # self.driver.find_element(By.CSS_SELECTOR,
        #                          "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()
        #
        # username_password_wrong = WebDriverWait(self.driver, 10).until(
        #     EC.visibility_of_element_located((By.CSS_SELECTOR, "div > div > div > span:nth-child(2)"))
        # )
        # print(username_password_wrong.text)
        # assert username_password_wrong.text == "您已经输入用户名或密码错误 1 次，在输入错误 4 次，账号将被锁定1小时！"

    def LoginSucTest(self):
        #成功登录
        login_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                              "#app > div > div.right > div.main > div:nth-child(3) > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input"))
        )
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").clear()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").clear()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(1) > div.ant-col.ant-form-item-control > div > div > span > span > input").send_keys(
            "xncs_lcl")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(2) > div.ant-col.ant-form-item-control > div > div > span > span > span.ant-input-password.ant-input-password-large.ant-input-affix-wrapper.ant-input-affix-wrapper-lg > input").send_keys(
            "12345678")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#app > div > div.right > div > div > form > div:nth-child(1) > div:nth-child(3) > div > div > div > button").click()

        self.driver.refresh()
        username_password_suc = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, "#app > div.head.fixed > div > div.nav > ul > li.ant-menu-overflow-item.ant-menu-item.ant-menu-item-selected.ant-menu-item-only-child > span > a"))
        )
        #因为再登录失败一次就要等几个小时，所以登陆失败的例子等其他例子都通过后再测试
        #self.driver.find_element(By.CSS_SELECTOR,"")

